package edu.hawaii.halealohacli.kmj;

import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.sensordata.jaxb.SensorData;

/**
 * This command prints out a timestamp and the current power for [tower | lounge] every [interval]
 *  seconds.  [interval] is an optional integer greater than 0 and defaults to 10 seconds. 
 *  Entering any character (such as a carriage return) stops this monitoring process and returns 
 *  the user to the command loop.  
 * 
 * @author Eldon Visitacion
 */
public class MonitorPower implements Command {
  
  /** power reading. */
  private long pow = 0;
  /** name of tower. */
  private String name;
  /** string for full timestamp.*/
  private String timeStamp;
  /** string for current time. */
  private String time;
  /** keeps track if it's a successful run (error checking). */
  private boolean successfulRun;
  
  /**
   * Constructor for MonitorPower.
   * @param client client to be used
   * @param towerLounge Location to be used
   */
  public MonitorPower(WattDepotClient client,String towerLounge) {  
    name = towerLounge;
    successfulRun = true;
    setAll(client, towerLounge);
  }
  /**
   * Sets all the needed values.
   * 
   * @param client client to be used
   * @param towerLounge Location to be used
   */
  private void setAll(WattDepotClient client,String towerLounge) {
    //get power of desired source
    try {
      SensorData sensorData = null;
      double power = 0;
      sensorData = client.getLatestSensorData(name);
      power = client.getLatestPowerConsumed(name);
      power = power / 1000;
      pow = (long)power;
      
      timeStamp = (sensorData.getTimestamp()).normalize().toString();
      timeStamp = timeStamp.substring(0,timeStamp.indexOf('T'));
      this.time = sensorData.getTimestamp().getHour() + ":" + sensorData.getTimestamp().getMinute() 
          + ":" + sensorData.getTimestamp().getSecond();
      if (power == 0) {
        //if(powerData == null){
        this.successfulRun = false;
        System.err.println("Unable to retrieve power data");
      }
    }
    catch (NotAuthorizedException e) {
      this.successfulRun = false;
      System.err.println("Not authorized to view source");
    }
    catch (ResourceNotFoundException e) {
      this.successfulRun = false;
      System.err.println("Source name not found");
    }
    catch (BadXmlException e) {
      this.successfulRun = false;
      System.err.println("server provided bad xml:" + e.toString());
    }
    catch (MiscClientException e) {
      this.successfulRun = false;
      System.err.println("problem encountered with server:" + e.toString());
    }
    catch (NullPointerException e) {
      this.successfulRun = false;
      System.err.println("there was nothing found!");
    }
  }
  
  /**
   * indicates to tester if run was successful.
   * @return result of program's execution.
   */
  public boolean getResult() {
    return this.successfulRun;
  }
  
  @Override
  public void printResult() {
    System.out.println( name + "'s power as of " + timeStamp + " " + time
                             + " is " + pow + " kW");
  }
  
  /**
   * Returns name of source being used.
   * 
   * @return Source (tower/lounge) name.
   */
  public String getName() {
    return this.name;
  }
  
  /**
   * Returns detected power usage of source in kWh.
   * 
   * @return Source (tower/lounge) power consumption for current day in kWh.
   */
  public long getPower() {
    return this.pow;
  }
}
